using FastCode.Models;
using System.Security.Claims;

namespace FastCode.Services
{
    public class UserService : BaseService
    {
        protected ISqlSugarClient db;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public UserService(ISqlSugarClient _db, IHttpContextAccessor httpContextAccessor)
        {
            db = _db;
            _httpContextAccessor = httpContextAccessor;
        }


        public async Task<User> GetCurrentUserAsync(string userId="")
        {
            if (string.IsNullOrEmpty(userId))
            {
                userId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
            }
            if (userId == null) {
                return null;
            }
            var user = await db.Queryable<User>().FirstAsync(x => x.Id == Convert.ToInt32(userId));
            return user;
        }
    }
}
